﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BussinessLogicLayer;
using DataAccessLayer;

namespace QLThongTinGiaiDauBongDa.Presentation.frmNguoiDung
{
    public partial class frmCapNhatPhanQuyen : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmCapNhatPhanQuyen()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmCapNhatPhanQuyen_Load(object sender, EventArgs e)
        {
            dgvNguoiDung.DataSource = NguoiDungBLL.LayTatCa();
            dgvNguoiDung.Columns["MaNguoiDung"].Visible = false;
            dgvNguoiDung.Columns["NgaySinh"].Visible = false;
            dgvNguoiDung.Columns["MatKhau"].Visible = false;
            dgvNguoiDung.Columns["CauHoiBiMat"].Visible = false;
            dgvNguoiDung.Columns["CauTraLoi"].Visible = false;
            dgvNguoiDung.Columns["TrangThai"].Visible = false;
            HienThiQuyen();
            
        }
        private void HienThiQuyen()
        {
            foreach (Quyen quyen in PhanQuyenBLL.LayQuyen().ToArray())
            {
                ListViewItem item = new ListViewItem();
                lvwphanquyen.Items.Add(item);
                item.SubItems.Add(quyen.TenQuyen);
                item.SubItems.Add(quyen.MoTa);
            }
        }

        private void KiemTraPhanQuyen()
        {
            lvwphanquyen.Items.Clear();
            HienThiQuyen();
            int MaND = Convert.ToInt32(dgvNguoiDung.CurrentRow.Cells["MaNguoiDung"].Value);
            for (int i = 0; i < PhanQuyenBLL.LayQuyen().Count; i++)
            {
                foreach (Quyen quyen in PhanQuyenBLL.BatChucNang(MaND).ToArray())
                {
                    if (lvwphanquyen.Items[i].SubItems[1].Text == quyen.TenQuyen.ToString())
                        lvwphanquyen.Items[i].Checked = true;
                }
            }
        }
        private void btnPhanQuyen_Click(object sender, EventArgs e)
        {
            int MaND = Convert.ToInt32(dgvNguoiDung.CurrentRow.Cells["MaNguoiDung"].Value);
            if (PhanQuyenBLL.XoaPhanQuyen(MaND))
            {
                for (int i = 0; i < PhanQuyenBLL.LayQuyen().Count; i++)
                {
                    if (lvwphanquyen.Items[i].Checked == true)
                    {
                        int MaQuyen = Convert.ToInt32(PhanQuyenBLL.LayMaQuyen(lvwphanquyen.Items[i].SubItems[1].Text).Select(ma => ma.MaQuyen).FirstOrDefault().ToString());
                        PhanQuyen entity = new PhanQuyen();
                        entity.MaPhanQuyen = 0;
                        entity.MaNguoiDung = MaND;
                        entity.MaQuyen = MaQuyen;
                        entity.KichHoat = true;
                        PhanQuyenBLL.PhanQuyen(entity);
                    }
                }
                MessageBox.Show("Phân quyền thành công!!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }   
            else
                MessageBox.Show("Phân quyền thất bại!!..Vui lòng thử lại.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void dgvNguoiDung_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            KiemTraPhanQuyen();
        }
    }
}